feat(package): add stage-override-to-pr-base script#55
Conversation
- @shiftcode/branch-utilities@4.1.0-pr181.0 - @shiftcode/publish-helper@4.0.1-pr181.0
- @shiftcode/branch-utilities@4.1.0-pr181.1 - @shiftcode/publish-helper@4.0.1-pr181.1
- @shiftcode/branch-utilities@4.1.0-pr181.2 - @shiftcode/publish-helper@4.1.0-pr181.0
… to avoid command log statement
- @shiftcode/branch-utilities@4.1.0-pr181.3 - @shiftcode/publish-helper@4.1.0-pr181.1
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new command-line script stage-override-to-pr-base to the branch-utilities package that determines branch override settings and PR status for CI/CD workflows. The script outputs environment variables to configure branch name overrides and PR detection.
Key changes:
- Adds new CLI script with yargs command-line interface
- Implements core function to detect PR status using GitHub CLI
- Updates package versions to prerelease format for testing
Reviewed Changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/branch-utilities/src/stage-override-to-pr-base.ts | New CLI entry point script with yargs argument parsing |
| packages/branch-utilities/src/scripts/stage-override-to-pr-base.function.ts | Core function implementing PR detection logic using GitHub CLI |
| packages/branch-utilities/src/scripts/index.ts | Export barrel for the new function |
| packages/branch-utilities/package.json | Adds binary entry point and yargs dependency |
| packages/publish-helper/package.json | Updates dependency versions to prerelease format |
| try { | ||
| const responseMsg = await stageOverrideToPrBase(args) | ||
| console.info(responseMsg) | ||
| }catch (err){ |
There was a problem hiding this comment.
Missing space after closing brace. Should be } catch (err) { to follow standard JavaScript formatting conventions.
| }catch (err){ | |
| } catch (err) { |
| export async function stageOverrideToPrBase( | ||
| options: StageOverrideToPrBaseOptions | Promise<StageOverrideToPrBaseOptions>, | ||
| ): Promise<string> { | ||
| const opts = await options | ||
|
|
||
| const gitHubPrJson = execSync('gh pr list --json headRefName,title,isDraft,closed', { encoding: 'utf8' }).trim() | ||
| const gitHubPrs = JSON.parse(gitHubPrJson) | ||
|
|
||
| const branchNameOverride = opts.branchNameOverride || process.env['GITHUB_BASE_REF'] | ||
| const isPrOverride = !!gitHubPrs.find((pr: any) => pr.headRefName === branchNameOverride) |
There was a problem hiding this comment.
Using any type defeats TypeScript's type safety. Consider defining a proper interface for the PR object with properties like headRefName, title, isDraft, and closed.
| export async function stageOverrideToPrBase( | |
| options: StageOverrideToPrBaseOptions | Promise<StageOverrideToPrBaseOptions>, | |
| ): Promise<string> { | |
| const opts = await options | |
| const gitHubPrJson = execSync('gh pr list --json headRefName,title,isDraft,closed', { encoding: 'utf8' }).trim() | |
| const gitHubPrs = JSON.parse(gitHubPrJson) | |
| const branchNameOverride = opts.branchNameOverride || process.env['GITHUB_BASE_REF'] | |
| const isPrOverride = !!gitHubPrs.find((pr: any) => pr.headRefName === branchNameOverride) | |
| interface GitHubPr { | |
| headRefName: string | |
| title: string | |
| isDraft: boolean | |
| closed: boolean | |
| } | |
| export async function stageOverrideToPrBase( | |
| options: StageOverrideToPrBaseOptions | Promise<StageOverrideToPrBaseOptions>, | |
| ): Promise<string> { | |
| const opts = await options | |
| const gitHubPrJson = execSync('gh pr list --json headRefName,title,isDraft,closed', { encoding: 'utf8' }).trim() | |
| const gitHubPrs: GitHubPr[] = JSON.parse(gitHubPrJson) | |
| const branchNameOverride = opts.branchNameOverride || process.env['GITHUB_BASE_REF'] | |
| const isPrOverride = !!gitHubPrs.find((pr: GitHubPr) => pr.headRefName === branchNameOverride) |
| return ` | ||
| export SC_OVERRIDE_BRANCH_NAME=${branchNameOverride} | ||
| export SC_OVERRIDE_IS_PR=${isPrOverride} | ||
| ` |
There was a problem hiding this comment.
The returned string contains leading whitespace that may cause issues when sourced as shell commands. Consider using a trimmed template literal or removing the indentation.
| return ` | |
| export SC_OVERRIDE_BRANCH_NAME=${branchNameOverride} | |
| export SC_OVERRIDE_IS_PR=${isPrOverride} | |
| ` | |
| return `export SC_OVERRIDE_BRANCH_NAME=${branchNameOverride} | |
| export SC_OVERRIDE_IS_PR=${isPrOverride} | |
| ` |
| ): Promise<string> { | ||
| const opts = await options | ||
|
|
||
| const gitHubPrJson = execSync('gh pr list --json headRefName,title,isDraft,closed', { encoding: 'utf8' }).trim() |
There was a problem hiding this comment.
The execSync call should include error handling for cases where the gh command fails (e.g., not authenticated, repository not found, or GitHub CLI not installed).
| const gitHubPrJson = execSync('gh pr list --json headRefName,title,isDraft,closed', { encoding: 'utf8' }).trim() | |
| let gitHubPrJson: string | |
| try { | |
| gitHubPrJson = execSync('gh pr list --json headRefName,title,isDraft,closed', { encoding: 'utf8' }).trim() | |
| } catch (error: any) { | |
| console.error( | |
| 'Failed to execute "gh pr list". Please ensure that the GitHub CLI (gh) is installed, you are authenticated, and you are in a valid repository.', | |
| ) | |
| if (error.stderr) { | |
| console.error('Error details:', error.stderr.toString()) | |
| } | |
| throw error | |
| } |
|
@dario-fazio closing as this is now part of https://github.com/shiftcode/sc-commons/pull/215 |
addresses: https://github.com/shiftcode/sc-commons/issues/181